//
//  iToast.m
//  OCQ_API_Test
//
//  Created by 故乡的云  on 2017/5/10.
//  Copyright © 2017年 故乡的云 . All rights reserved.
//

#import "iToast.h"
@interface iToast ()

@property(nonatomic, copy) NSString *text;
/// 默认为：UILabel
@property(nonatomic, weak) UIView *showView;
@property(nonatomic, assign) NSInteger queueCount;

@end

@implementation iToast
/*
static  iToast*_itoast;
// 重写allocWithZone:方法，在这里创建唯一的实例
+ (instancetype)allocWithZone:(struct _NSZone *)zone
{
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{ // 保证线程安全
        _itoast = [super allocWithZone:zone];
    });
    return _itoast;
}

// 提供1个类方法让内部访问唯一的实例
+ (instancetype)shared
{
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        _itoast = [[self alloc] init];
        _itoast.backgroundColor = [[UIColor darkGrayColor] colorWithAlphaComponent:0.75];
        _itoast.layer.cornerRadius = 5.0f;
    });
    _itoast.queueCount = 0;
    _itoast.alpha = 1.0f;
    return _itoast;
}
*/

- (id)initWithFrame:(CGRect)frame {
    if (self = [super initWithFrame:frame]) {
        self.backgroundColor = [[UIColor darkGrayColor] colorWithAlphaComponent:0.75];
        self.layer.cornerRadius = 5.0f;
        self.queueCount = 0;
    }
    self.alpha = 1.0f;
    return self;
}

- (void) setText:(NSString *)text {
    _text = text;
    
    UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(12, 10, 100, 10)];
    self.showView = label;
    label.text = text;
    label.numberOfLines = 1;
    label.font = [UIFont systemFontOfSize:13];
    label.textColor = [UIColor whiteColor];
    label.textAlignment = NSTextAlignmentCenter;
    label.backgroundColor = [UIColor clearColor];
    [self addSubview:label];
    [label sizeToFit];
    _queueCount++;
    [self layoutIfNeeded];
    
    __weak iToast *weakSelf = self;
    [UIView animateWithDuration:0.3 delay:2.0 options:UIViewAnimationOptionCurveEaseIn animations:^{
        weakSelf.alpha = 0;
     } completion:^(BOOL finished){
         if (_queueCount == 1) {
             [weakSelf removeFromSuperview];
         }
         _queueCount--;
     }];
}

- (void)layoutSubviews {
    [super layoutSubviews];
    
    self.frame = CGRectMake(0, 0, self.showView.frame.size.width+24, self.showView.frame.size.height+20);
    CGFloat w = [UIScreen mainScreen].bounds.size.width * 0.5f;
    CGFloat h = [UIScreen mainScreen].bounds.size.height * 0.5f;
    self.center = CGPointMake(w, h);
    self.showView.center = CGPointMake(self.bounds.size.width * 0.5, self.bounds.size.height * 0.5);
}

+ (void)showiToast:(UIView *)inView text:(NSString *)text {
    [self showiToast:inView text:text Y:([UIScreen mainScreen].bounds.size.height - kBottomMargin)];
}

+ (void)showiToast:(UIView *)inView text:(NSString *)text Y:(CGFloat)y {
    inView = inView == nil ? [UIApplication sharedApplication].keyWindow : inView; // 默认 keyWindow
    iToast *toast = [[iToast alloc] init];//[self shared];
    [inView addSubview:toast];
    [toast setText:text];
    CGFloat x = toast.center.x;
    y = y > 0 ? y : ([UIScreen mainScreen].bounds.size.height - kBottomMargin);
    toast.center = CGPointMake(x, y);
}

+ (void)showiToast:(UIView *)inView view:(UIView *)view Y:(CGFloat)y{
    inView = inView == nil ? [UIApplication sharedApplication].keyWindow : inView; // 默认 keyWindow
    iToast __block*toast = [[iToast alloc] init];
    [inView addSubview:toast];
    toast.showView = view;
    [toast addSubview:view];
    CGFloat x = inView.bounds.size.width / 2;
    [toast layoutIfNeeded];
    y = y > 0 ? y : (inView.bounds.size.height / 2);
    toast.center = CGPointMake(x, y);
    
    [UIView animateWithDuration:0.3 delay:2.0 options:UIViewAnimationOptionCurveEaseIn animations:^{
        toast.alpha = 0;
    } completion:^(BOOL finished){
        if (toast.queueCount == 1) {
            [inView removeFromSuperview];
        }
        toast.queueCount--;
    }];

}

@end
